package mypack;

import java.io.IOException;
//import java.util.StringTokenizer;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class InitPopMapper extends Mapper<LongWritable, Text, IntWritable, Chromosome> 
{

	public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException
	{
		//Global.gen.readFile();
		Global.gen.readTSPLIBFile();
		
		Population population = new Population();
		population.createPopulation();
		population.calculateAllPathLengths();
		int keyVal = Integer.parseInt(value.toString());
		
		//statistics
		//Global.gen.printMatrix(keyVal);
		
		IntWritable key1 = new IntWritable(keyVal);
		System.out.println("PopulationId: "+keyVal);
		for (int i = 0; i < Global.POPULATION_SIZE; i++) 
		{
			Chromosome val = population.currentPopulation[i];
			val.populationId = keyVal;
			val.generationNumber = 0;
			val.prevPop = keyVal;
			System.out.println("fitness:"+val.mFitness);
			context.write(key1, val);
		}
		
		population.printPopulation(0,keyVal);
		population.writeBestOneToHDFS(0, keyVal);
	}
}
